package com.uplooking.pms.system.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.uplooking.pms.base.controller.BaseController;
import com.uplooking.pms.common.util.CommonUtil;
import com.uplooking.pms.common.util.Constant;
import com.uplooking.pms.system.entity.User;
import com.uplooking.pms.system.service.IUserService;

@Controller
public class IndexController extends BaseController {

	@Autowired
	private IUserService userService;

	@RequestMapping("/")
	public String index() {
		return "index";
	}

	@RequestMapping("/toLogin")
	public String toLoing() {
		return "login";
	}

	@RequestMapping("/login")
	public String login(String name, String pwd, ModelMap map, HttpSession session) throws Exception {
		// 根据名字查找
		Wrapper wrapper = Condition.create().eq("name", name);
		User user = userService.selectOne(wrapper);
		if (!CommonUtil.isBlank(user)) {
			if (CommonUtil.isEquals(pwd, user.getPwd())) {

				// 登录时间
				user.setLogintime(CommonUtil.date6());
				// 获取ip
				user.setLoginip(getAddr());

				if (userService.updateById(user)) {
					// 匹配成功
					session.setAttribute("pmsuser", user);
					return "index";
				}
				map.put("msg", "登录失败");
			} else {
				// 密码不对
				map.put("msg", "密码错误");
			}
		} else {
			// 没有该帐号
			map.put("msg", "用户不存在");
		}
		return "login";
	}

	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.removeAttribute(Constant.SESSION_USER_KEY);
		return "login";
	}

	@RequestMapping("toTimeOut")
	public String toTimeOut() {
		return "timeoutConfirm";
	}

	@RequestMapping("inc")
	public String inc() {
		return "inc";
	}
}
